package com.dingmouren.example.layoutmanagergroup.widget;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.dingmouren.layoutmanagergroup.echelon.EchelonLayoutManager;

public class DraaggableView extends FrameLayout {
    private boolean showLog = true;
    private String tag = "DraaggableView";

    private RecyclerView recyclerView;
    private EchelonLayoutManager mLayoutManager;

    public static int curItemCount;
    public DraaggableView(@NonNull Context context) {
        super(context);
    }

    public DraaggableView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DraaggableView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        if(showLog) {
//            Log.e(tag, "======== onInterceptTouchEvent: ");
//        }
//        return false;
//    }

    public void getViewTop(int top) {
        if (showLog) {
            Log.e(tag, "=========== getViewTop: "+top);
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        getParent().requestDisallowInterceptTouchEvent(true);
        return super.dispatchTouchEvent(ev);
    }



    static boolean startFlip = false;
    static int lockFlip = 0;
    float y;
    float mY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if(showLog) {
            Log.e(tag, "======== onTouchEvent: ");
        }
        switch (event.getAction()){
                case MotionEvent.ACTION_UP:
                    if (startFlip) break;  //正在动画禁止操作
                    if(showLog) {
                        Log.e(tag, "======== onTouchEvent ACTION_UP bottomItemPosition: "+EchelonLayoutManager.bottomItemPosition+" curItemCount: "+curItemCount);
                    }
                    if (showLog) {
                        Log.e(tag, "========ACTION_UP 3333 y-mY: "+(y-mY));
                    }
                    if (y-mY>20 && !startFlip) { //保证是onFlip且没有进行中的动画
                        if (curItemCount >1) {
                            startRotation90();
                            this.animate().alpha(0f).setDuration(1500);
                        }
                        mY = y;
                    }

                break;
            case MotionEvent.ACTION_MOVE:
                if (startFlip) break;  //正在动画禁止操作
//                if(showLog) {
//                    Log.e(tag, "======== onTouchEvent ACTION_MOVE top: "+event.getY());
//                }
                if (curItemCount >1) {
                    y = event.getY();

                    if (showLog) {
                        Log.e(tag, "========ACTION_MOVE 3333 y-mY: "+(y-mY));
                    }
                    if(y-mY>20 && !startFlip){ //保证是onFlip且没有进行中的动画
                        startRotation(y-mY);
                    }
                }
                break;
            case MotionEvent.ACTION_DOWN:
                if (startFlip) break;  //正在动画禁止操作

                if (EchelonLayoutManager.bottomItemPosition==1) {
                    Log.e(tag,"============bottomItemPosition lockFlip before: "+lockFlip);
                    lockFlip = lockFlip+1;
                    Log.e(tag,"============bottomItemPosition lockFlip after: "+lockFlip);
                }
                if(showLog) {
                    Log.e(tag, "======== onTouchEvent ACTION_DOWN bottomItemPosition： "+EchelonLayoutManager.bottomItemPosition+" lockFlip: "+lockFlip);
                }

                break;
        }

        return true;
    }

    float rotation;
    private void startRotation(float moveFlex){
//        Log.e(tag,"===========22222222222222222222222222222moveFlex=="+moveFlex);
        if(rotation>=150){
            this.setVisibility(View.INVISIBLE);
            rotation = 0;
            mY = 0;
            startFlip = false; //动画结整
            curItemCount = curItemCount - 1;
            if (showLog) {
                Log.e(tag, "================ curItemCount: "+curItemCount);
            }
            if(countDownTimer!=null){
                countDownTimer.cancel();
            }
            if(animationEndListener!=null){
                animationEndListener.onAnimationEnd();

//                    滑动完成500ms后恢复可见且角度还原
//                    this.setRotationX(rotation);
//                    this.setVisibility(View.VISIBLE);
                startDelayTimer();
            }
            return;
        }
        if(moveFlex<20){
            rotation = rotation + 1;
        }else if(moveFlex<40){
            rotation = rotation + 2;
        }else if(moveFlex>40){
            rotation = rotation + 3;
        }

        this.setPivotX(this.getWidth()/2); //View旋转中心点的X坐标
        this.setPivotY(this.getHeight()/2.64f);

        int distance = 16000;
        float scale = getResources().getDisplayMetrics().density * distance;
        this.setCameraDistance(scale);   //设置镜头视角

        if(rotation>150){
            rotation = 150;
        }
        this.setRotationX(-rotation);  //开始旋转

    }

    CountDownTimer countDownTimer;
    private void startRotation90(){
        EchelonLayoutManager.isSlide = false;
        startFlip = true;
        countDownTimer =  new CountDownTimer(2000,10){

            @Override
            public void onTick(long l) {
                if(showLog) {
                    Log.e(tag,"========= startRotation90 onTick");
                }
                startRotation(100);
            }

            @Override
            public void onFinish() {

            }
        }.start();
    }

    CountDownTimer delayTimer;
    private void startDelayTimer(){

        delayTimer =  new CountDownTimer(1000,500){

            @Override
            public void onTick(long l) {
//                if(showLog) {
//                    Log.e(tag,"========= startRotation90 onTick");
//                }
            }

            @Override
            public void onFinish() {
                DraaggableView.this.setRotationX(0);
                DraaggableView.this.setVisibility(View.VISIBLE);
                DraaggableView.this.setAlpha(1f);
                delayTimer = null;
            }
        }.start();
    }

    public AnimationEndListener animationEndListener;

    public interface AnimationEndListener {
        void onAnimationEnd();
    }

    public void setOnAnimationEndListener(AnimationEndListener animationEndListener){
        this.animationEndListener = animationEndListener;
    }

}
